<template>
  <el-dialog :close-on-click-modal='false' v-drag :title="record.id?$t('编辑') : $t('添加')" :v-if="dialogVisible" :visible.sync="dialogVisible" width="600px" @close='close' top="10vh">
    <div class="dialogContentBox">
      <el-form ref="form" :model="record" label-width="80px" label-position="left" :rules="rules" style="margin:0 20px;">
        <el-form-item :label="$t('名称')" prop="name">
          <el-input v-emoji v-model="record.name" size='small' ref="name"></el-input>
        </el-form-item>
        <el-form-item :label="$t('描述')" prop="desc">
          <el-input v-emoji v-model="record.desc" size='small' ref="desc" type="textarea" :placeholder="$t('请输入内容')" @keydown.native="handleKeyCode($event)" :autosize="{ minRows: 2, maxRows: 4 }"></el-input>
        </el-form-item>
        <el-button type="success" size="small" @click="addAsset" class="addAsset">{{$t('添加资产对象')}}</el-button>

        <el-table size='mini' :data="memberList" stripe border style="width: 100%;margin-top: 5px;">
          <el-table-column prop='name' :label="$t('名称')" align="center" min-width="100">
            <template slot-scope="scope">
              <tooltip :html="scope.row" :content="scope.row" class="wid190" :refName="scope.row">
              </tooltip>
            </template>
          </el-table-column>
          <el-table-column fixed="right" :label="$t('操作')" width="100" align="center">
            <template slot-scope="scope">
              <el-button @click.native.prevent="deleteRow(scope.$index, record.member_id, memberList)" :class="'del' + scope.$index" type="danger" plain size="mini">
                {{$t('删除')}}
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <asset-dialog ref="refAssetDialogVisible" @addAssetForTable='addAssetForTable'></asset-dialog>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit" class="addSubmit">{{$t('确 定')}}</el-button>
      <el-button @click="close" class="addClose">{{$t('取 消')}}</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { addAssetGroup, editAssetGroup, getAssetGroupInfo } from '@/api/list/assets/assetGroup'
import AssetDialog from './AssetDialog'
import { inputClass, texttextClass } from '@/utils/input-class.js'
export default {
  name: 'AssetGroupDialog',
  components: {
    AssetDialog
  },
  data () {
    var validateName = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else {
        callback()
      }
    }

    return {
      dialogVisible: false,
      record: {
        id: null,
        name: '',
        member_id: [],
        desc: ''
      },
      recordCopy: {
        id: null,
        name: '',
        member_id: [],
        desc: ''
      },
      memberList: [],
      rules: {
        name: [{ required: false, validator: validateName, trigger: 'blur' }]
      }
    }
  },
  watch: {
    'record.desc': {
      handler (newVal) {
        this.record.desc = newVal.replace(/[\r\n]/g, '')
      },
      immediate: true
    }
  },
  methods: {
    init (data) {
      this.dialogVisible = true
      if (data) {
        getAssetGroupInfo({ name: data.name }).then(res => {
          this.record = Object.assign(this.record, res.info[0])
          this.memberList = this.record.member
        })
      }
      this.$nextTick(() => {
        const data = ['name']
        const textdata = ['desc']
        inputClass(this.$refs, data)
        texttextClass(this.$refs, textdata)
      })
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.record.member_id && this.record.member_id.length > 0) {
            if (!this.record.id) {
              addAssetGroup(this.record).then(res => {
                if (res.status === 0) {
                  this.$message({
                    message: this.$t('添加成功'),
                    type: 'success'
                  })
                }
                this.close()
                this.$emit('listData')
              })
            } else {
              editAssetGroup(this.record).then(res => {
                if (res.status === 0) {
                  this.$message({
                    message: this.$t('编辑成功'),
                    type: 'success'
                  })
                }
                this.close()
                this.$emit('listData')
              })
            }
          } else {
            this.$messages.error(this.$t('资产对象不能为空'))
          }
        }
      })
    },
    close () {
      this.dialogVisible = false
      this.memberList = []
      this.record = Object.assign({}, this.recordCopy)
      this.$refs.form.resetFields()
    },
    addAsset () {
      this.$nextTick(() => {
        this.$refs.refAssetDialogVisible.init()
      })
    },
    addAssetForTable (data) {
      for (let item of data) {
        if (!this.record.member_id.includes(item.id)) {
          this.record.member_id.push(item.id)
          this.memberList.push(item.name)
        }
      }
    },
    deleteRow (index, rows, rowsList) {
      rows.splice(index, 1)
      rowsList.splice(index, 1)
    },
    handleKeyCode (event) {
      if (event.keyCode === 13) {
        event.preventDefault() // 阻止浏览器默认换行操作
        return false
      }
    }
  }
}
</script>
<style scoped>
.dialogContentBox {
  max-height: calc(80vh - 184px);
  overflow-y: auto;
}
</style>
